package test;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Producer {

	public static void main(String[] args) throws Exception{
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		ConnectionFactory factory = (ConnectionFactory)context.getBean("targetConnectionFactory");
		Destination queue = (Destination)context.getBean("queueDestination");
		javax.jms.Connection con = factory.createConnection();
		con.start();
		
		JSONObject json = new JSONObject();
		json.put("timestamp", "20160229");
		json.put("name", "john");
		json.put("ip", "192.168.3.25");
		String s = json.toString();
		
		Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageProducer pro = session.createProducer(queue);
		TextMessage tmsg = session.createTextMessage(s);
		pro.send(tmsg);
		
		pro.close();
		session.close();
		con.close();
		
	}

}
